﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ContactManager.Common;
using System.Data.OleDb;

namespace ContactManager
{
    public partial class YearWorkIncPayFrm : AlSkin.AlForm.AlBaseForm
    {
        DateTime now = DateTime.Now;

        public YearWorkIncPayFrm()
        {
            InitializeComponent();
        }

        private void YearWorkIncPay_Load(object sender, EventArgs e)
        {
            this.labelContactName.Text = Constant.TITLE + GlobleUser.USER_NAME;
            
            //初始化最近联系人
            this.combRecent.Items.Clear();
            string recentSql = "select top 18 c.c_id,c.c_name,max(t.t_time) from contacters c,traces t where c.c_id = t.t_c_id group by c.c_id,c.c_name order by max(t.t_time) desc";
            DataSet recentDs = AccessHelper.ExecuteQuery(recentSql);
            foreach (DataRow dr in recentDs.Tables[0].Rows)
            {
                ComboBoxItem ci = new ComboBoxItem();
                ci.Text = Convert.ToString(dr["c_name"]);
                ci.Value = dr["c_id"];
                this.combRecent.Items.Add(ci);
            }


            setDate();

        }

        private void setDate()
        {
            //初始化日期
            this.labDate.Text = "日期：" + now.ToString("yyyy");
            DateTime firstDay = new DateTime(now.Year, 1, 1);
            DateTime lastDay = new DateTime(now.Year, 12, 31);
            
            //初始化收入表
            string incSql = "select sum(i_contract_num),sum(i_get_num),format(i_date,'yyyy/mm') from incwork where i_date >=#" + firstDay.ToString("yyyy/MM/dd") + "# and i_date <=#" + lastDay.ToString("yyyy/MM/dd") + "# group by format(i_date,'yyyy/mm')";
            DataSet incDs = AccessHelper.ExecuteQuery(incSql);
            this.dataGridViewInc.Rows.Clear();
            foreach (DataRow dr in incDs.Tables[0].Rows)
            {
                double contract = Convert.ToDouble(dr[0].ToString());
                double get = Convert.ToDouble(dr[1].ToString());
                string date = Convert.ToDateTime(dr[2].ToString()).ToString("MM");

                this.dataGridViewInc.Rows.Add(0, date, contract.ToString("0.00"), get.ToString("0.00"));
            }

            //初始化支出表
            this.dataGridViewPay.Rows.Clear();
            string paySql = "select format(p_date,'yyyy/mm'),sum(p_traffic),sum(p_tour),sum(p_serve),sum(p_office_supplies),sum(p_meet),sum(p_express),sum(p_phone),sum(p_water),sum(p_rent) from paywork where p_date >=#" + firstDay.ToString("yyyy/MM/dd") + "# and p_date<=#" + lastDay.ToString("yyyy/MM/dd") + "# group by format(p_date,'yyyy/mm')";
            DataSet payDs = AccessHelper.ExecuteQuery(paySql);
            //更新grid
            
            foreach (DataRow dr in payDs.Tables[0].Rows)
            {
                string date = Convert.ToDateTime(dr[0].ToString()).ToString("MM");
                double traffic = Convert.ToDouble(dr[1].ToString());
                double tour = Convert.ToDouble(dr[2].ToString());
                double serve = Convert.ToDouble(dr[3].ToString());
                double office = Convert.ToDouble(dr[4].ToString());
                double meet = Convert.ToDouble(dr[5].ToString());
                double express = Convert.ToDouble(dr[6].ToString());
                double phone = Convert.ToDouble(dr[7].ToString());
                double water = Convert.ToDouble(dr[8].ToString());
                double rent = Convert.ToDouble(dr[9].ToString());
                double total = traffic + tour + serve + office + meet + express + phone + water + rent;
                this.dataGridViewPay.Rows.Add(0, date, traffic, tour, serve, office, meet, express, phone, water, rent, total);
            }
            

            //初始化统计
            double inc = 0;//实收金额
            double incDeal = 0;//合同金额
            double pay = 0;//支出

            string iSql = "select sum(i_get_num),sum(i_contract_num) from incwork where i_date >=#" + firstDay.ToString("yyyy/MM/dd") + "# and i_date <=#" + lastDay.ToString("yyyy/MM/dd") + "#";
            DataSet iDs = AccessHelper.ExecuteQuery(iSql);
            if (iDs.Tables[0].Rows[0][0].ToString().Length > 0)
            {
                inc = Convert.ToDouble(iDs.Tables[0].Rows[0][0].ToString());
            }
            if (iDs.Tables[0].Rows[0][1].ToString().Length > 0)
            {
                incDeal = Convert.ToDouble(iDs.Tables[0].Rows[0][1].ToString());
            }

            string pSql = "select sum(p_traffic+p_tour+p_serve+p_office_supplies+p_meet+p_express+p_phone+p_water+p_rent) from paywork where p_date >=#" + firstDay.ToString("yyyy/MM/dd") + "# and p_date<=#" + lastDay.ToString("yyyy/MM/dd") + "#";
            DataSet pDs = AccessHelper.ExecuteQuery(pSql);
            if (iDs.Tables[0].Rows[0][0].ToString().Length > 0)
            {
                pay = Convert.ToDouble(pDs.Tables[0].Rows[0][0].ToString());
            }

            this.labSum.Text = "本年合同收入：" + incDeal.ToString("0.00") + " 实收入金额：" + inc.ToString("0.00") + " 支出：" + pay.ToString("0.00") + " 结余：" + (inc - pay).ToString(".00");
        }

        private void btnPreviousMonth_Click(object sender, EventArgs e)
        {
            now = now.AddYears(-1);
            setDate();
        }

        private void btnNow_Click(object sender, EventArgs e)
        {
            now = DateTime.Now;
            setDate();
        }

        private void btnNextMonth_Click(object sender, EventArgs e)
        {
            if (now <= DateTime.Now.AddYears(-1))
            {
                now = now.AddYears(1);
                setDate();
            }
        }

        private void YearWorkIncPay_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("您确定要退出吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
            {
                HomeFrm.ywip = null;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void exportData_Click(object sender, EventArgs e)
        {
            ExcelUtil.exportYearWorkInc(this.dataGridViewPay, this.dataGridViewInc, this.labSum.Text, this.progressBarMain);
        }

        private void combRecent_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBoxItem recent = (ComboBoxItem)this.combRecent.SelectedItem;
            TracesForm tf = new TracesForm(recent.Value.ToString());
            tf.ShowDialog();
        }

        private void dataGridViewPay_DoubleClick(object sender, EventArgs e)
        {
            if (this.dataGridViewPay.CurrentRow != null)
            {
                string month = this.dataGridViewPay.CurrentRow.Cells["p_date"].Value.ToString();
                DateTime dt = new DateTime(now.Year, Convert.ToInt32(month), 1);
                WorkIncPayFrm wip = new WorkIncPayFrm(dt);
                wip.ShowDialog();
            }
        }

        private void dataGridViewInc_DoubleClick(object sender, EventArgs e)
        {
            if (this.dataGridViewInc.CurrentRow != null)
            {
                string month = this.dataGridViewInc.CurrentRow.Cells["i_date"].Value.ToString();
                DateTime dt = new DateTime(now.Year, Convert.ToInt32(month), 1);
                WorkIncPayFrm wip = new WorkIncPayFrm(dt);
                wip.ShowDialog();
            }
        }

    }
}
